<?php
class rchatController extends  appController{
	public function index()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$cfg = Model('rchat')->get_cfg();
		$list=Model('rchat')->listUnchecked($page,$cfg);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$this->assign('count',$list['count']);
		$this->assign('passCount',$list['passCount']);
		$this->assign('page',$page);
		
		$staggerTime = intval($cfg['staggerTime']);
		//uvpv
		$visitortime = $cfg["visitortime"];
		$visitornow = yf_time()+$staggerTime-$visitortime;
		$vtime =strtotime(date('Y-m-d H:00:00',$visitornow));
		//获取uv与pv
		$result = Model("rchat")->get_uv_pv_2M($visitornow,$vtime,$visitortime);
		$this->assign("pv", $result['pv']);
		$this->assign("uv", $result['uv']);
			
		//获取上一周期弹幕池数量
// 		$lastCycleNum = Model('rchat')->get_cfg("");
		$this->assign("lastCycleNum",$cfg['lastCycleNum']);
			
		//刷新时间
		$timestamp =intval($cfg['timestamp']);
		$refresh = intval($cfg['refreshtime']);
		$status = intval($cfg['passstatus']);
		$time = yf_time();
		$this->assign('timeout',($refresh-($time-($timestamp-$staggerTime))%$refresh));
		$this->assign('refreshtime',$refresh);
		$this->assign('status',$status);
		$this->assign("timestamp", $timestamp-$staggerTime);
		$this->assign("now", $time);
			
		$this->display('index.html');
	}
	
	public function batchPass()
	{
		$uids = $_POST['uids'];
		$result=array('error'=>0,'content'=>'批量审核通过失败');
		if(Model('rchat')->updateStatus($uids,3)>0)
		{
			$result['error'] = 1;
			$result['content'] = "批量审核通过成功";
		}
		print_r(json_encode($result));
		exit();
	}
	public function batchNopass()
	{
		$uids = $_POST['uids'];
		$result=array('error'=>0,'content'=>'批量审核不通过失败');
		if(Model('rchat')->updateStatus($uids,2)>0)
		{
			$result['error'] = 1;
			$result['content'] = "批量审核不通过成功";
		}
		print_r(json_encode($result));
		exit();
	}
	//清除弹幕池数据
	public function cleanChatPool()
	{
		$result=array('error'=>0,'content'=>'清除成功');
		$fp = fopen(ROOT_PATH."chatR.json", "w");
		fwrite($fp, json_encode(array()));
		fclose($fp);
		Model("rchat")->cleanChatPool();
		print_r(json_encode($result));
	}
	
	public function update()
	{
		$status = isset($_GET['status'])?intval($_GET['status']):0;
		if(!$status){
			Model('rchat')->updateComment();
		}else{
			Model('rchat')->updateCommentPass();
		}
		$result['error'] = 1;
		$result['content'] = "刷新成功";
		print_r(json_encode($result));
		exit();
	}
	public function updatestatus()
	{
		$status = isset($_GET['status'])?intval($_GET['status']):0;
		$status = Model('rchat')->update_pass_status($status);
		echo $status;
	}
	
	public function clearNopass()
	{
		$result=Model('rchat')->clearNopass();
		print_r(json_encode($result));
	}
	
	public function silenceUser()
	{
		$wxid=isset($_POST['wxid'])?$_POST['wxid']:'';
		$status=isset($_POST['status'])?intval($_POST['status']):0;
		$result=array('error'=>0,'content'=>'禁言失败');
		if($wxid&&$status){
			if(Model('rchat')->silenceUser($wxid,$status)){
				$result['error'] = 1;
				$result['content'] = "禁言成功";
			}
		}
		print_r(json_encode($result));
	}
	public function delRecord()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('rchat')->del_record($v);
			}
		}
		else
		{
			$result=Model('rchat')->del_record($id);
		}
		print_r(json_encode($result));
	}
	public function addDigest()
	{
		$id = isset($_POST['id'])?$_POST['id']:"";
		$res = Model('rchat')->addDigest($id);
		print_r(json_encode($res));
	}
	/*****************************************弹幕配置************************/
	public function config()
	{
		$chat = Model('rchat');
		$app = Model('app');
		$config = $app->get_cfg();
		$this->assign('config', $config);
		$this->display('config.html');
	}
	public function saveConfig()
	{
		$info['refreshtime'] = isset($_POST['refreshtime'])?intval($_POST['refreshtime']):15;
		$info['capacityPool'] = isset($_POST['capacityPool'])?intval($_POST['capacityPool']):50;
		$info['chatPool'] = isset($_POST['refreshtime'])?intval($_POST['refreshtime']):15;
		$info['capacityPooleach'] = isset($_POST['capacityPooleach'])?intval($_POST['capacityPooleach']):25;
		$info['isOrderBy'] = isset($_POST['isOrderBy'])?intval($_POST['isOrderBy']):0;
		$info['isRepeat'] = isset($_POST['isRepeat'])?intval($_POST['isRepeat']):0;
		$info['isShowHongbao'] = isset($_POST['isShowHongbao'])?intval($_POST['isShowHongbao']):0;
		$info['isShowAlert'] = isset($_POST['isShowAlert'])?intval($_POST['isShowAlert']):0;
		$info['commentMinuteMax'] = isset($_POST['commentMinuteMax'])?intval($_POST['commentMinuteMax']):0;
		$info['commentDayMax'] = isset($_POST['commentDayMax'])?intval($_POST['commentDayMax']):0;
		$info['chatMaxNum'] = isset($_POST['chatMaxNum'])?intval($_POST['chatMaxNum']):0;
		$info['chatIntegral'] = isset($_POST['chatIntegral'])?intval($_POST['chatIntegral']):0;
		$info['digestNum'] = isset($_POST['digestNum'])?intval($_POST['digestNum']):0;
		$info['placeholder'] = isset($_POST['placeholder'])?$_POST['placeholder']:"";
		$info['digestTime'] = isset($_POST['digestTime'])?intval($_POST['digestTime']):1;
		$info['lastHB'] = isset($_POST['lastHB'])?intval($_POST['lastHB']):7;
		if(Model('rchat')->saveConfig($info))
		{
			$result['error'] = 0;
			$result['content'] = Lang('VALID_OPERATION');
		}else{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	/***********************************************禁言用户****************************************************************/
	public function silence()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$silence_list=Model('rchat')->silence_list($page);
		$this->assign('silence_list',$silence_list['data']);
		$this->assign('user_list_page',$silence_list['pagelink']);
		$this->assign('count',$silence_list['count']);
		$this->display('silence_list.html');
	}
	
	public function delSilence(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('rchat')->del($v,"chat_silence_user");
			}
		}
		else
		{
			$result=Model('rchat')->del($id,"chat_silence_user");
		}
		print_r(json_encode($result));
	}
	/**********************************关键词拦截******************************************************/
	public function keyword(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$keyword_list=Model('chat')->keyword_list($page);
		$this->assign('keyword_list',$keyword_list['data']);
		$this->assign('user_list_page',$keyword_list['pagelink']);
		$this->assign('count',$keyword_list['count']);
		$this->display('keyword_list.html');
	}
	
	public function saveKeyword(){
		$info['id'] = isset($_POST['id'])?intval($_POST['id']):0;
		$info['keyword']  = isset($_POST['keyword'])?$_POST['keyword']:"";
		$result=array('error'=>1,'content'=>'关键词修改失败');
		if($info['keyword']){
			if($info['id']){//修改
				$res=Model('chat')->update($info,array("table"=>'chat_keyword',"id"=>$info['id']));
			}else{//新增
				$info['addtime'] = yf_time();
				$res=Model('chat')->insert($info,'chat_keyword');
			}
			if($res){
				$result['error']=0;
				$result['content']='编辑成功';
			}
		}
		print_r(json_encode($result));
	}
	
	public function del_keyword(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('rchat')->del($v,"chat_keyword");
			}
		}
		else
		{
			$result=Model('rchat')->del($id,"chat_keyword");
		}
		print_r(json_encode($result));
	}
	
	/********************************特殊弹幕******************************/
	/**
	 * 特殊弹幕
	 */
	public function specialComment(){
	
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$special_list=Model('rchat')->special_list($page);
		$this->assign('ur_here','>特殊弹幕管理');
		$this->assign('special_list',$special_list['data']);
		$this->assign('user_list_page',$special_list['pagelink']);
		$this->display('special_list.html');
	}
	
	public function add_chat(){
		$this->assign('ur_here','>新增特殊项弹幕');
		$this->assign('action','create_chat');
		$group = Model('rchat')->getGroups($id);
	
		$this->assign('group',$group);
		$lucky = Model('rchat')->getLucky($id);
		$this->assign('lucky',$lucky);
		$this->display('special_edit.html');
	
	}
	public function create_chat(){
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['subtitle']=isset($_POST['subtitle'])?$_POST['subtitle']:'';
		$info['group_id']=isset($_POST['group_id'])?$_POST['group_id']:'0';
		$info['hongbao_id']=isset($_POST['hongbao_id'])?$_POST['hongbao_id']:'0';
		$info['source_url']=isset($_POST['source_url'])?$_POST['source_url']:'';
		$info['pic_url']=isset($_POST['pic_url'])?$_POST['pic_url']:'';
		$info['background_color']=isset($_POST['background_color'])?$_POST['background_color']:'';
		$info['color']=isset($_POST['color'])?$_POST['color']:'';
		$info['addtime'] = yf_time();
		$result=array('error'=>1,'content'=>'');
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		if( mb_strlen($_POST['title'],'UTF8')>15){
			$data['error']=1;
			$data['content']=Lang('标题长度不可超过14字');
			$this->showMsg($data);
		}
		if($ad_id=Model('rchat')->insert($info,'chat_special'))
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_ADD');
		}
		else
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	public function edit_chat(){
		$this->assign('ur_here','>编辑特殊项弹幕');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$chat=Model('rchat')->get_chat_special($id);
	
		$group = Model('rchat')->getGroups($id);
	
		//var_dump($chat);
		$this->assign('group',$group);
		$this->assign('chat',$chat);
		$this->assign('id',$id);
		$this->assign('action','update_chat');
	
		$lucky = Model('rchat')->getLucky($id);
		$this->assign('lucky',$lucky);
		$this->display('special_edit.html');
	}
	public function update_chat(){
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['group_id']=isset($_POST['group_id'])?$_POST['group_id']:'0';
		$info['hongbao_id']=isset($_POST['hongbao_id'])?$_POST['hongbao_id']:'0';
		$info['subtitle']=isset($_POST['subtitle'])?$_POST['subtitle']:'';
		$info['source_url']=isset($_POST['source_url'])?$_POST['source_url']:'';
		$info['background_color']=isset($_POST['background_color'])?$_POST['background_color']:'';
	
		$info['color']=isset($_POST['color'])?$_POST['color']:'color1';
	
		$info['pic_url']=isset($_POST['pic_url'])?$_POST['pic_url']:'';
		$data=array('error'=>1,'content'=>'');
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		if( mb_strlen($_POST['title'],'UTF8')>15){
			$data['error']=1;
			$data['content']=Lang('标题长度不可超过14字');
			$this->showMsg($data);
		}
		$result=Model('rchat')->update($info,array('table'=>'chat_special','id'=>$id));
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
	
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	public function del_chat()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('rchat')->del_chat($v);
			}
		}
		else
		{
			$result=Model('rchat')->del_chat($id);
		}
		print_r(json_encode($result));
	}
	/***********************************************以下是规则管理的方法****************************************************************/
	public function rule_list()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$special_id=isset($_GET['bid'])?intval($_GET['bid']):'';
		$rule_list=Model('rchat')->rule_list($page,$special_id);
		$this->assign('special_id',$special_id);
		$this->assign('rule_list',$rule_list['data']);
		$this->assign('user_list_page',$rule_list['pagelink']);
		$this->assign('count',$rule_list['count']);
		$this->display('rule_list.html');
	}
	public function addRule()
	{
		$sid=isset($_GET['sid'])?intval($_GET['sid']):'';
		$this->assign('sid',$sid);
		$this->assign('action','create_rule');
		$this->display('rule_edit.html');
	}
	
	public function create_rule()
	{
		$info['rule_name']=isset($_POST['rule_name'])?$_POST['rule_name']:'';
		$info['status']=isset($_POST['status'])?intval($_POST['status']):1;
		$info['time']=isset($_POST['time'])?intval($_POST['time']):1;
		$info['comment_num']=isset($_POST['comment_num'])?intval($_POST['comment_num']):0;
		$info['showtime']=isset($_POST['showtime'])?intval($_POST['showtime']):0;
		$info['special_id']=isset($_POST['sid'])?$_POST['sid']:0;
		$info['specific_time']=isset($_POST['specific_time'])?strtotime($_POST['specific_time']):0;
		$info['addtime'] = yf_time();
		$data=array('error'=>1,'content'=>'');
		if($_POST['rule_name']=='')
		{
			$data['error']=1;
			$data['content']=Lang('规则名不能为空');
			$this->showMsg($data);
		}
		$result=Model('rchat')->insert($info,'chat_special_rule');
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
	
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	public function editRule()
	{
		$this->assign('ur_here','>编辑特殊项弹幕规则');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$sid=isset($_GET['sid'])?intval($_GET['sid']):'';
		$rule=Model('rchat')->get_chat_special_rule($id);
		$this->assign('sid',$sid);
		$this->assign('rule',$rule);
		$this->assign('id',$id);
		$this->assign('action','update_rule');
		$this->assign('time',date('Y-m-d H:i:s',yf_time()));
		$this->display('rule_edit.html');
	}
	
	public function update_rule()
	{
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['rule_name']=isset($_POST['rule_name'])?$_POST['rule_name']:'';
		$info['status']=isset($_POST['status'])?intval($_POST['status']):1;
		$info['time']=isset($_POST['time'])?intval($_POST['time']):1;
		$info['comment_num']=isset($_POST['comment_num'])?intval($_POST['comment_num']):0;
		$info['showtime']=isset($_POST['showtime'])?intval($_POST['showtime']):0;
		$info['special_id']=isset($_POST['sid'])?$_POST['sid']:0;
		$info['specific_time']=isset($_POST['specific_time'])?strtotime($_POST['specific_time']):0;
		$info['addtime'] = yf_time();
		$data=array('error'=>1,'content'=>'');
		if($_POST['rule_name']=='')
		{
			$data['error']=1;
			$data['content']=Lang('规则名不能为空');
			$this->showMsg($data);
		}
		$result=Model('rchat')->update($info,array("table"=>'chat_special_rule',"id"=>$id));
		if($result)
		{
			$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
	
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($data);
	}
	
	public function delRule(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('rchat')->del($v,"chat_special_rule");
			}
		}
		else
		{
			$result=Model('rchat')->del($id,"chat_special_rule");
		}
		print_r(json_encode($result));
	}
	
	public function yaohome(){
		$this->assign("homes",Model("rchat")->get_home_url());
		$this->display("yaohome_list.html");
	}
	
	public function saveImmediately(){
		$data = array("error"=>1,"content"=>"立即对话失败");
		if(!$_POST['immediately']){
			$data['error']=1;
			$data['content']="对话内容为空";
			$this->showMsg($data);
		}
		$info['comment'] = $_POST['immediately'];
		$info['status'] = 8;//立即对话中未经过立即审核的状态位;
		$info['addtime'] = yf_time();
		$res = Model("rchat")->insert($info,"chat_record"); 
		if($res){
			$data['error']=0;
			$data['content']="立即对话成功";
		}
		$this->showMsg($data);
	}
	
	/***********************************************以下是评论记录统计的方法****************************************************************/
	public function record(){
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$startTime = isset($_REQUEST['startTime'])?strtotime($_REQUEST['startTime']):mktime(0,0,0,date("m"),date("d"),date("Y"));
		$endTime = isset($_REQUEST['endTime'])?strtotime($_REQUEST['endTime']):0;
		$nickname = isset($_REQUEST['nickname'])?$_REQUEST['nickname']:'';
		$comment = isset($_REQUEST['comment'])?$_REQUEST['comment']:'';
		if($endTime!=0){
			$endTime = strtotime(date( 'Y-m-d 23:59:59', $endTime));
		}else{
			$endTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		}
		$status = isset($_REQUEST['status'])?$_REQUEST['status']:6;
		$total['startTime'] = $startTime;
		$total['endTime'] = $endTime;
		$total['nickname'] = $nickname;
		$total['comment'] = $comment;
		if($status!=6){
			$total['status'] = $status;
		}
		$list=Model('rchat')->list_record($page,$total);
		$this->assign('sql_list',$list['data']);
		$this->assign('list_page',$list['pagelink']);
		$total['count'] = $list['count'];
		$total['pass_count'] = $list['pass_count'];
		$total['nopass_count'] = $list['nopass_count'];
		$total['passing_count'] = $list['passing_count'];
		$this->assign('total',$total);
		$this->assign('page',$page);
		$this->display('chat_record.html');
	}
	
	public function temCheck(){
		$id = isset($_POST['id'])?$_POST['id']:0;
		if($id){
			$result = Model('rchat')->updateStatus($id,1);
			// 			$result = Model('chat')->updateStatus($id,0);
			if($result){
				$result = array('error'=>0,'content'=>'添加成功');
			}else{
				$result = array('error'=>1,'content'=>'添加失败');
			}
		}else{
			$result = array('error'=>1,'content'=>'添加失败');
		}
		print_r(json_encode($result));
	}
}